<?php

class Admin_AdmissionController extends Zend_Controller_Action {
	protected $_flashMessenger = null;
	public function init() {
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			if ($auth->getIdentity ()->role != 'administrator') {
				$this->_helper->redirector ( 'index', 'auth', 'default' );
			}
		} else {
			$this->_helper->redirector ( 'index', 'auth', 'default' );
		}
		$this->_helper->layout->setLayout ( 'admin' );
		$this->_flashMessenger = $this->_helper->getHelper ( 'FlashMessenger' );
		$this->initView ();
	}
	
	public function indexAction() {
		// action body
		$request = $this->getRequest ();
		$data = $request->getParams ();
		// get class list from class table
		

		$moClass = new Application_Model_ClassModel ();
                $moAdmission = new Application_Model_Academic();
		$class_list = $moClass->fetchAll ();
                $aAcademic_list = $moAdmission->fetchAll();
		$this->view->class_list = $class_list->toArray ();
                $this->view->academic_list = $aAcademic_list;
		$this->view->messages = $this->_flashMessenger->getMessages();
		if ($_POST) {
			$password = md5 ( '123456' );
			$user_data = array ('username' => $data ['p_username'], 'password' => $password, 'role' => 'parent' );
			$users_model = new Application_Model_Users ();
			$user_id = $users_model->save ( $user_data );
			
			$parent_data = array ('first_name' => $data ['p_firstname'], 'last_name' => $data ['p_lastname'], 'email' => $data ['p_email'], 'phoneno' => $data ['p_mobileno'], 'user_id' => $user_id );
			$parent_model = new Application_Model_Parents ();
			$parent_id = $parent_model->save ( $parent_data );
			
			$student_data = array ('parent_id' => $parent_id, 'fname' => $data ['s_firstname'], 'lname' => $data ['s_lastname'], 'admission_no' => $data ['s_admission_no'], 'address' => $data ['s_address'], 'phoneno' => $data ['p_mobileno'] );
			$student_model = new Application_Model_Students ();
			$student_id = $student_model->save ( $student_data );
			
			$student_classdata = array ('student_id' => $student_id, 'class_id' => $data ['s_class'],'academic_id'  => $data['s_academic'] );
			$studentclass_model = new Application_Model_StudentClass ();
			$studentclass_model->save ( $student_classdata );
			$this->_flashMessenger->addMessage('Record Saved!');
			$this->_redirect('/admin/admission');
		}
	}

}

